package cn.ljy.uem.service.impl;

import cn.ljy.authority.dao.mapper.SysDepartmentMapper;
import cn.ljy.authority.model.SysDepartment;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.authority.utils.MyObjectUtils;
import cn.ljy.uem.dao.SchoolDepartmentMapper;
import cn.ljy.uem.dao.SchoolMajorMapper;
import cn.ljy.uem.model.SchoolDepartment;
import cn.ljy.uem.model.query.SchoolDepartmentQuery;
import cn.ljy.uem.model.query.SchoolMajorQuery;
import cn.ljy.uem.service.SchoolDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门模块的业务层接口
 *
 * @author 凡渊
 * @date 2022/07/18 10:40:35
 */
@Service
@Slf4j
public class ISchoolDepartmentService extends IBaseService<SchoolDepartment, SchoolDepartmentQuery> implements SchoolDepartmentService {
    @Autowired
    private SysDepartmentMapper sysDepartmentMapper;

    @Autowired
    private SchoolDepartmentMapper schoolDepartmentMapper;

    @Autowired
    private SchoolMajorMapper schoolMajorMapper;
    @Override
    protected void beforeGet(Serializable id) {

    }

    @Override
    protected void beforeUpdate(SchoolDepartment schoolDepartment) {

    }

    @Override
    protected void beforeSave(SchoolDepartment schoolDepartment) {

    }

    @Override
    protected void beforeDelete(Serializable[] ids) {

    }

    @Override
    public List<SchoolDepartment> findAll(SchoolDepartmentQuery query) {
        List<SchoolDepartment> all = super.findAll(query);
        for (SchoolDepartment schoolDepartment:
             all) {
            if (schoolDepartment.getId()!=null){
                SysDepartment sysDepartment=sysDepartmentMapper.findById(schoolDepartment.getSysDepartmentId());
                schoolDepartment.setSysDepartment(sysDepartment);
            }
        }
        return all;
    }

    /**
     * 设置关联属性
     * @param department
     */
    private void setParam(SchoolDepartment department){
        //添加相关关联属性
        //部门下的专业数
        SchoolMajorQuery schoolMajorQuery=new SchoolMajorQuery();
        schoolMajorQuery.setSchoolDepartment(department);
        department.setMajorNumber(schoolMajorMapper.count(schoolMajorQuery));
    }

    /**
     * 采用非递归形式，为树形集合设置关联属性
     * 说明：
     * 1.应该是树形结构，即每个点应该都带children
     * 2.如果不是树形结构（例如直接查询集合列表），则children不应该带值，否则会严重影响效率
     * @param tree
     */
    private void setTreeDepartmentParam(List<SchoolDepartment> tree){
        tree.stream().forEach(department -> {
            //添加相关关联属性
            setParam(department);
            List<SchoolDepartment> childrens=department.getChildren();
            List<SchoolDepartment> childrensTmp=new ArrayList<>();//临时存储对象
            while(childrens.size()>0){
                for(SchoolDepartment departmentChildren:childrens){
                    //添加相关关联属性
                    setParam(departmentChildren);
                    childrensTmp.addAll(departmentChildren.getChildren());
                }
                //遍历结束后
                childrens=new ArrayList<>();//清空集合，不用用clear，否则会把原集合的引用对象移除
                childrens.addAll(childrensTmp);//重置children集合
                childrensTmp=new ArrayList<>();//清空临时集合，不能用clear，需要重新更换地址
            }
        });
    }
//    @Override
    public List<SchoolDepartment> findTree(SchoolDepartmentQuery schoolDepartmentQuery) {
        List<SchoolDepartment> tree=new ArrayList();
        if(MyObjectUtils.checkObjAllFieldsIsNull((schoolDepartmentQuery))){//如果
            tree=schoolDepartmentMapper.findTree();
        }else{
            tree = schoolDepartmentMapper.findAll(schoolDepartmentQuery);
        }
        //设置关联属性
        setTreeDepartmentParam(tree);
        return tree;
    }

    private List<SchoolDepartment> buildTree(List<SchoolDepartment> list){
        List<SchoolDepartment> tree=new ArrayList();
        for(SchoolDepartment schoolDepartment:list){
            if(tree.size()==0){//如果是第一次添加
                tree.add(schoolDepartment);
                break;
            }
            for(SchoolDepartment department:tree){
                if(department.getSysDepartmentId().equals(schoolDepartment.getId())){
                }
            }

        }
        return tree;
    }
}
